package com.hunktimes.wholesale.ui.tabs.dashboard

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.base.BaseMainFragment
import com.hunktimes.wholesale.databinding.FragmentCartBinding
import com.hunktimes.wholesale.extension.setImageUrl
import com.hunktimes.wholesale.mock.goods
import com.hunktimes.wholesale.net.model.MockGoods

class CartFragment : BaseMainFragment(R.layout.fragment_cart) {

    override var titleRes = R.string.title_cart

    private val cartViewModel by viewModels<CartViewModel>()
    lateinit var viewBinding: FragmentCartBinding
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding = FragmentCartBinding.bind(view)
    }

    override fun onStart() {
        super.onStart()
        initCart()
        initRecommend()

        viewBinding.settlementTv.setOnClickListener { _ ->
            rootNavController.navigate(R.id.navigation_create_order)
        }
    }

    val cartAdapter = CartAdapter()
    fun initCart() {
        val cartLm = LinearLayoutManager(requireContext())
        cartLm.orientation = LinearLayoutManager.VERTICAL
        viewBinding.cartRcv.layoutManager = cartLm
        viewBinding.cartRcv.adapter = cartAdapter

        val data = mutableListOf<MockGoods>()
//        for (i in 1..2) {
//            data.add("購物車$i")
//        }
        repeat(2) {
            data.add(goods.random())
        }
        cartAdapter.data = data
    }

    val recommendAdapter = CartRecommendAdapter()
    fun initRecommend() {
        val recommendLLm = GridLayoutManager(requireContext(), 2)
        recommendLLm.orientation = GridLayoutManager.VERTICAL
        viewBinding.cartRecommentRcv.layoutManager = recommendLLm
        viewBinding.cartRecommentRcv.adapter = recommendAdapter
        val data = mutableListOf<MockGoods>()
//        for (i in 1..3) {
//            data.add("購物車推薦$i")
//        }
        repeat(3) {
            data.add(goods.random())
        }
        recommendAdapter.data = data
    }

}

class CartAdapter : BaseQuickAdapter<MockGoods, BaseViewHolder>(R.layout.item_cart) {
    override fun convert(holder: BaseViewHolder, item: MockGoods) {
        val coverIv = holder.getView<ImageView>(R.id.goods_cover_iv)
        coverIv.setImageUrl(item.imageUrl)
        holder.setText(R.id.goods_title_tv, item.name)
        holder.setText(R.id.goods_price_tv, item.price)

    }

}

class CartRecommendAdapter : BaseQuickAdapter<MockGoods, BaseViewHolder>(R.layout.item_recommend) {
    override fun convert(holder: BaseViewHolder, item: MockGoods) {
//        val itemViewBinding = ItemRecommendBinding.bind(holder.itemView)
//        itemViewBinding.itemRecommendCoverIv.setImageUrl(item)
        val coverIv = holder.getView<ImageView>(R.id.item_recommend_cover_iv)
        coverIv.setImageUrl(item.imageUrl)
        holder.setText(R.id.item_recommend_title_tv, item.name)
        holder.setText(R.id.item_recommend_desc_tv, item.desc)
        holder.setText(R.id.item_recommend_price_tv, item.price)
    }

}